package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.PublishImageModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/2.
 */

public class PublishImageListAdapter extends RecyclerView.Adapter<PublishImageListAdapter.Holder> {

    private List<PublishImageModel> imageList;
    public Context context;
    private int limit;

    public PublishImageListAdapter(List<PublishImageModel> list, int limit) {
        this.limit = limit;
        imageList = new ArrayList<>();
        imageList.add(new PublishImageModel());
        if (list != null) {
            imageList.addAll(0, list);
        }
    }

    public void refresh(List<PublishImageModel> list) {
        List<PublishImageModel> removeList = new ArrayList<>();
        removeList.addAll(imageList.subList(0, imageList.size() - 1));
        imageList.removeAll(removeList);
        if (list != null) {
            imageList.addAll(0, list);
        }
        notifyDataSetChanged();
    }

    public void add(PublishImageModel image) {
        int startPosition = imageList.size() - 1;
        imageList.add(startPosition, image);
        notifyItemInserted(startPosition);
        notifyItemChanged(imageList.size() - 1);
    }

    public void add(List<PublishImageModel> list) {
        if(list != null && !list.isEmpty()) {
            int startPosition = imageList.size() - 1;
            imageList.addAll(startPosition, list);
            notifyDataSetChanged();
        }
    }

    public ArrayList<PublishImageModel> getImageList() {
        ArrayList<PublishImageModel> result = new ArrayList<>();
        result.addAll(imageList.subList(0, imageList.size() - 1));
        return result;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View rootView = LayoutInflater.from(context).inflate(R.layout.recycler_item_publish_cos_image_list, parent, false);
        return new Holder(rootView);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        if (!TextUtils.isEmpty(imageList.get(position).getContentPath())) {
            Glide.with(context)
                    .load(imageList.get(position).getContentPath())
                    .dontAnimate()
                    .dontTransform()
                    .error(R.drawable.im_default)
                    .centerCrop()
                    .into(holder.image);
            holder.remove.setVisibility(View.VISIBLE);
        } else {
            holder.image.setImageResource(R.drawable.im_add_publish_image);
            holder.remove.setVisibility(View.GONE);
        }
        holder.image.setVisibility(position == limit ? View.GONE : View.VISIBLE);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if (position == imageList.size() - 1) {
                    //点击的是添加图片按钮
                    if (listener != null) {
                        listener.onAddImageClicked();
                    }
                } else {
                    //点击的是图片
                    if (listener != null) {
                        listener.onImageContentClicked();
                    }
                }
            }
        });
        holder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                imageList.remove(position);
                notifyItemRemoved(position);
                notifyItemChanged(imageList.size() - 1);
            }
        });
    }

    @Override
    public int getItemCount() {
        return imageList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.remove)
        ImageView remove;


        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnImageClickedListener listener;

    public interface OnImageClickedListener {
        void onAddImageClicked();

        void onImageContentClicked();
    }

    public void setOnAddImageClickedListener(OnImageClickedListener listener) {
        this.listener = listener;
    }
}
